package com.microframework.base.core.handler;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.microframework.base.core.exception.NotAuthorizedException;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理器
 * 
 * 处理安全相关异常
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    
    @ExceptionHandler(NotAuthorizedException.class)
    public ResponseEntity<Map<String, Object>> handleNotAuthorizedException(Exception e) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 401);
        response.put("msg", e.getMessage());
        return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED);
    }
    
    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<Map<String, Object>> handleException(Exception e) {
        Map<String, Object> response = new HashMap<>();
        response.put("code", 500);
        response.put("msg", e.getMessage());
        return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
    }
    
    
} 